package math;

import java.util.StringTokenizer;


public class Vector2f {
	public static final Vector2f ZERO = new Vector2f();
	
	public float x, y;
	
	public Vector2f() {
		set(0, 0);
	}
	
	public Vector2f(float v) {
		this(v, v);
	}
	
	public Vector2f(float x, float y) {
		set(x, y);
	}
	
	public Vector2f(Vector2f vec) {
		set(vec);
	}
	
	public Vector2f(Vector3f vec) {
		set(vec);
	}
	
	public Vector2f(Vector4f vec) {
		set(vec);
	}	
	
	public Vector2f add(float x, float y) {
		this.x += x;
		this.y += y;
		return this;
	}
	
	public Vector2f add(Vector2f vec) {
		return add(vec.x, vec.y);
	}

    public static Vector2f add(Vector2f a, Vector2f b) {
        return new Vector2f(a.x + b.x, a.y + b.y);
    }
	
	public Vector2f sub(float x, float y) {
		this.x -= x;
		this.y -= y;
		return this;
	}
	
	public Vector2f sub(Vector2f vec) {
		return sub(vec.x, vec.y);
	}

	public Vector2f mult(float f) {
		return mult(f, f);
	}
	
	public Vector2f mult(float x, float y) {
		this.x *= x;
		this.y *= y;
		return this;
	}

	
	public Vector2f mult(Vector2f vec) {
		return mult(vec.x, vec.y);
	}
	
    public static Vector2f mult(Vector2f a, Vector2f b) {
        return new Vector2f(a.x * b.x, a.y * b.y);
    }
	
	public Vector2f divide(float f) {
		return divide(f, f);
	}
	
	public Vector2f divide(float x, float y) {
		this.x /= x;
		this.y /= y;
		return this;
	}
	
	public Vector2f divide(Vector2f vec) {
		return divide(vec.x, vec.y);
	}
	
	public float length() {
		return (float)Math.sqrt(x * x + y * y);
	}
	
	public static float distance(Vector2f vec1, Vector2f vec2) {
		Vector2f vec = vec1.sub(vec2);
		return vec.length();
	}
	
	public Vector2f copy() {
		return new Vector2f(this);
	}

	public float[] toArray() {
		return new float[]{x,y};
	}
	
	public Vector2f reset() {
		x = y = 0;
		return this;
	}

	public Vector2f normalize() {
		float length = length();
		if (length > 0) {
			x /= length;
			y /= length;
		}
		return this;
	}
	
	public float dot(Vector2f vec) {
		return x * vec.x + y * vec.y;
	}

	
	@Override
	public boolean equals(Object o) {
		if(o instanceof Vector2f) {
			Vector2f v = (Vector2f)o;
			return x == v.x && y == v.y;
		}
		
		return false;
	}
	
	public Vector2f set(float x, float y) {
		this.x = x;
		this.y = y;
		return this;
	}
	
	public Vector2f set(Vector2f vec) {
		return set(vec.x, vec.y);
	}
	
	public Vector2f set(Vector3f vec) {
		return set(vec.x(), vec.y());
	}
	
	public Vector2f set(Vector4f vec) {
		return set(vec.x, vec.y  );
	}
	
	public String println() {
		return "" + x + " " + y;
	}
	
	public static Vector2f parse(StringTokenizer tokenizer) {
		float x = Float.valueOf(tokenizer.nextToken());
		float y = Float.valueOf(tokenizer.nextToken());
		return new Vector2f(x, y);
	}

	//--------------------------------------------------------------todo
	
}
